package com.shire.movienizerdroid.utils;

import java.util.Collection;
import java.util.Iterator;

/**
 * @author yhernandezdiaz
 */
public final class CollectionUtils {

	public static String collectionToString(Collection<String> collection, String separator) {
		final StringBuilder sb = new StringBuilder();
		for (String item : collection) {
			if (sb.length() > 0)
				sb.append(separator);
			sb.append(item);
		}
		return sb.toString();
	}

	public static String collectionToString(Collection<String> collection, String separator, String format) {
		final StringBuilder sb = new StringBuilder();
		for (String item : collection) {
			if (sb.length() > 0)
				sb.append(separator);
			sb.append(String.format(format, item));
		}
		return sb.toString();
	}

	public static String collectionToString(Collection<String> collection, String separator, int max, String overflow) {
		final StringBuilder sb = new StringBuilder();
		final Iterator<String> iter = collection.iterator();
		int i = 0;
		while (iter.hasNext() && (++i < max)) {
			if (sb.length() > 0)
				sb.append(separator);
			sb.append(iter.next());
		}
		if (iter.hasNext())
			sb.append(overflow);
		return sb.toString();
	}
	
	public static String collectionToString(Collection<String> collection, String separator, String format, int max, String overflow) {
		final StringBuilder sb = new StringBuilder();
		final Iterator<String> iter = collection.iterator();
		int i = 0;
		while (iter.hasNext() && (++i < max)) {
			if (sb.length() > 0)
				sb.append(separator);
			sb.append(String.format(format, iter.next()));
		}
		if (iter.hasNext())
			sb.append(overflow);
		return sb.toString();
	}

	private CollectionUtils() {}
}
